package com.boot.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.entity.TUser;
import com.boot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Chunsheng.Zhang
 * @date 2021/1/15 10:31
 * @description : 测试条件构造器的使用
 * 当查询条件复杂的时候,我们可以使用MP的条件构造器,
 */
@RestController
public class TestWrapperController {

    @Autowired
    private IUserService userService;





    /**
     * MP扩展演示
     * @Author Sans
     * @CreateTime 2019/6/8 16:37
     * @Return Map<String,Object> 返回数据
     */
    @RequestMapping("/getUserListPlus")
    public Map<String,Object> getInfoListPage(){
        //初始化返回类
        Map<String,Object> result = new HashMap<>();


        //查询年龄等于18岁的用户
        //等价SQL: SELECT id,name,age,skill,evaluate,fraction FROM user_info WHERE age = 18
        QueryWrapper<TUser> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().eq(TUser::getAge,18);
        List<TUser> TUserList1 = userService.list(queryWrapper1);
        result.put("userAge18", TUserList1);



        //查询年龄大于5岁的用户且小于等于18岁的用户
        //等价SQL: SELECT id,name,age,skill,evaluate,fraction FROM user_info WHERE age > 5 AND age <= 18
        QueryWrapper<TUser> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.lambda().gt(TUser::getAge,5);
        queryWrapper2.lambda().le(TUser::getAge,18);
        List<TUser> TUserList2 = userService.list(queryWrapper2);
        result.put("userAge5_18", TUserList2);


        //模糊查询realName字段带有"张"的数据,并按照年龄降序
        //等价SQL: SELECT id,name,age,skill,evaluate,fraction FROM user_info WHERE skill LIKE '%画%' ORDER BY age DESC
        QueryWrapper<TUser> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.lambda().like(TUser::getRealName,"张");
        queryWrapper3.lambda().orderByDesc(TUser::getAge);
        List<TUser> TUserList3 = userService.list(queryWrapper3);
        result.put("userRealName", TUserList3);


        //模糊查询名字带有"小"或者年龄大于18的学生
        //等价SQL: SELECT id,name,age,skill,evaluate,fraction FROM user_info WHERE name LIKE '%小%' OR age > 18
        QueryWrapper<TUser> queryWrapper4 = new QueryWrapper<>();
        queryWrapper4.lambda().like(TUser::getRealName,"小");
        queryWrapper4.lambda().or().gt(TUser::getAge,18);
        List<TUser> TUserList4 = userService.list(queryWrapper4);
        result.put("studentOr", TUserList4);


        //查询身份证不为null的学生,并且分页
        //等价SQL: SELECT id,name,age,skill,evaluate,fraction FROM user_info WHERE evaluate IS NOT NULL LIMIT 0,5
        IPage<TUser> page = new Page<>();
        page.setCurrent(1);
        page.setSize(5);
        QueryWrapper<TUser> queryWrapper5 = new QueryWrapper<>();
        queryWrapper5.lambda().isNotNull(TUser::getIdCard);
        page = userService.page(page,queryWrapper5);
        result.put("studentPage",page);
        return result;
    }

}
